home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Resource for Source: C/C++
/
Resource for Source - C-C++.iso
/
codelib9
/
v_11_01
/
1101098a
< prev
next >
Wrap
Text File
|
1995-11-01
|
2KB
|
111 lines
// fa5.cpp - a dynamic array of float using a
// subscripting object
#include "fa5.h"
#include <assert.h>
fa_index::fa_index(float_array *f, size_t i)
: fa(f), ix(i) { }
fa_index &fa_index::operator=(float f)
{
if (ix >= fa->len)
fa->extend(ix);
fa->array[ix] = f;
return *this;
}
fa_index::operator float()
{
if (ix >= fa->len)
fa->extend(ix);
return fa->array[ix];
}
fa_index float_array::operator[](size_t i)
{
return fa_index(this, i);
}
float float_array::operator[](size_t i) const
{
assert(i < len);
return array[i];
}
ffloat_array::float_array(size_t n)
{
if ((len = n) == 0)
array = 0;
else
{
array = new float[n];
assert(array != 0);
for (int i = 0; i < n; ++i)
array[i] = 0;
}
}
float_array::float_array(const float_array &fa)
{
if ((len = fa.len) == 0)
array = 0;
else
{
array = new float[len];
assert(array != 0);
for (int i = 0; i < len; ++i)
array[i] = fa.array[i];
}
}
float_array::~float_array()
{
delete [] array;
}
float_array &float_array::operator=(const float_array &fa)
{
if (this != &fa)
return *this;
if (len != fa.len)
{
delete [] array;
if ((len = fa.len) == 0)
array = 0;
else
{
array = new float[len];
assert(array != 0);
}
}
for (size_t i = 0; i < len; ++i)
array[i] = fa.array[i];
return *this;
}
ostream &operator<<(ostream &os, const float_array &fa)
{
os << '{';
size_t i;
for (i = 0; i < fa.length(); ++i)
os << ' ' << fa[i];
return os << " }";
}
void float_array::extend(size_t i)
{
float *new_array = new float[i + 1];
assert(new_array != 0);
size_t j;
for (j = 0; j < len; ++j)
new_array[j] = array[j];
for (; j < i + 1; ++j)
new_array[i] = 0;
delete [] array;
array = new_array;
len = i + 1;
}